package com.breaktime1903.stinky_eggs;

import net.minecraft.core.particles.ItemParticleOption;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.ThrowableItemProjectile;
import net.minecraft.world.entity.projectile.ThrownEgg;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.Level.ExplosionInteraction;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;

public class ThrownSuperStinkyEgg extends ThrowableItemProjectile {
    public ThrownSuperStinkyEgg(EntityType<? extends ThrownEgg> p_37473_, Level p_37474_) {
        super(p_37473_, p_37474_);
    }

    public ThrownSuperStinkyEgg(Level p_37481_, LivingEntity p_37482_) {
        super(EntityType.EGG, p_37482_, p_37481_);
    }

    public ThrownSuperStinkyEgg(Level p_37476_, double p_37477_, double p_37478_, double p_37479_) {
        super(EntityType.EGG, p_37477_, p_37478_, p_37479_, p_37476_);
    }

    public void handleEntityEvent(byte p_37484_) {
        if (p_37484_ == 3) {
            double d0 = 0.08D;

            for (int i = 0; i < 8; ++i) {
                this.level().addParticle(new ItemParticleOption(ParticleTypes.ITEM, this.getItem()), this.getX(),
                        this.getY(), this.getZ(), ((double) this.random.nextFloat() - 0.5D) * 0.08D,
                        ((double) this.random.nextFloat() - 0.5D) * 0.08D,
                        ((double) this.random.nextFloat() - 0.5D) * 0.08D);
            }
        }

    }

    protected void onHitEntity(EntityHitResult _entityHitResult) {
        super.onHitEntity(_entityHitResult);
        // 造成50~100点的随机伤害
        float damage = random.nextFloat() * 50.0F + 50.0F;
        this.level().playSound((Player)null, this.getX(), this.getY(), this.getZ(), StinkyEggsSoundEvents.ON_HIT_ENTITY.get(), SoundSource.BLOCKS, 0.5F, 1.0F);
        _entityHitResult.getEntity().hurt(this.damageSources().thrown(this, this.getOwner()), damage);
    }

    protected void onHit(HitResult _hitResult) {
        super.onHit(_hitResult);
        
        // 确保爆炸在服务端实现
        if(!this.level().isClientSide())
        {
            // 有70%的几率产生一般的爆炸，威力够足
            // 有30%的几率变成哑弹（没错，就是搞你一波
            float randomNum = random.nextInt(0,10);
            if(randomNum > 3)
            {
                this.level().playSound((Player)null, this.getX(), this.getY(), this.getZ(), StinkyEggsSoundEvents.ON_EXPLOSION.get(), SoundSource.BLOCKS, 1.0F, 1.0F);
                this.level().explode(this, this.getX(),this.getY(),this.getZ(),3.0F, false,ExplosionInteraction.TNT);
            }
            else
            {
                this.level().playSound((Player)null, this.getX(), this.getY(), this.getZ(), StinkyEggsSoundEvents.ON_FAILED_EXPLOSION.get(), SoundSource.BLOCKS, 1.0F, 1.0F);
            }
            this.discard();
        }
        
    }

    protected Item getDefaultItem() {
        return Items.EGG;
    }
}
